package pe.com.bbva.modpre.persistencia.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import pe.com.bbva.modpre.dominio.quartz.QrtzTriggers;
import pe.com.bbva.modpre.persistencia.TriggersDAO;
import pe.com.bbva.modpre.utileria.Busqueda;

@Repository
@SuppressWarnings("serial")
public class TriggersHibernate extends BaseHibernate<QrtzTriggers, Long> 
implements TriggersDAO {
	
	@Autowired
	public TriggersHibernate(SessionFactory sessionFactory){
		setSessionFactory(sessionFactory);
	}

	@Override
	public List<QrtzTriggers> buscarTriggers(QrtzTriggers qrtzTriggers) {
		Busqueda filtro = Busqueda.forClass(QrtzTriggers.class);
		if (qrtzTriggers != null) {
			if(qrtzTriggers.getTriggerGroup()!=null && StringUtils.isNotBlank(qrtzTriggers.getTriggerGroup())){
				filtro.add(Restrictions.ilike("triggerGroup",
							"%" + qrtzTriggers.getTriggerGroup() + "%"));
			}
			if(qrtzTriggers.getTriggerName()!=null && StringUtils.isNotBlank(qrtzTriggers.getTriggerName())){
				filtro.add(Restrictions.ilike("triggerName",
							"%" + qrtzTriggers.getTriggerName() + "%"));
			}
			if (qrtzTriggers.getDescription() != null && StringUtils.isNotBlank(qrtzTriggers.getDescription())) {
				filtro.add(Restrictions.ilike("description", "%" + qrtzTriggers.getDescription() + "%"));
			}
			if (qrtzTriggers.getTriggerType() != null && StringUtils.isNotBlank(qrtzTriggers.getTriggerType())) {
				filtro.add(Restrictions.ilike("triggerType", "%" + qrtzTriggers.getTriggerType() + "%"));
			}
			
			if (qrtzTriggers.getPriority() != null && StringUtils.isNotBlank(qrtzTriggers.getPriority().toString())) {
				filtro.add(Restrictions.eq("priority", qrtzTriggers.getPriority()));
			}
			if (qrtzTriggers.getJobGroup() != null && StringUtils.isNotBlank(qrtzTriggers.getJobGroup())) {
				filtro.add(Restrictions.eq("jobGroup", qrtzTriggers.getJobGroup()));
			}
			if (qrtzTriggers.getJobGroup() != null && StringUtils.isNotBlank(qrtzTriggers.getJobGroup())) {
				filtro.add(Restrictions.eq("jobName", qrtzTriggers.getJobName()));
			}
			if(qrtzTriggers.getTriggerState()!=null && StringUtils.isNotBlank(qrtzTriggers.getTriggerState())){
				filtro.add(Restrictions.ilike("triggerState", "%" + qrtzTriggers.getTriggerState() + "%"));
			}
		}
		return buscar(filtro);
	}

}
